//
//  LBWEmoticon.swift
//  weibo
//
//  Created by Limonwei on 15/12/15.
//  Copyright © 2015年 heima. All rights reserved.
//

//表情数据的模型

import UIKit

class LBWEmoticon: NSObject, NSCoding {
    
    //表情描述文字
    var chs: String?
    //图片名字
    var png: String?
    //表情类型
    var type: String? {
        didSet {
            //1表示emoji表情
            if type == "1" {
                isEmoji = true
            }
        }
    }
    
    //Emoji表情的字符串
    var code: String?
    //图片表情的前半段路径
    var path: String? {
        didSet{
            if png != nil {
                fullPath = "\(path!)/\(png!)"
            }
        }
    }
    
    //是否是Emoji表情
    var isEmoji: Bool = false
    
    //完整的地址
    var fullPath: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //归档  保存表情数据
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(png, forKey: "png")
        aCoder.encodeObject(type, forKey: "type")
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeObject(path, forKey: "path")
        aCoder.encodeObject(fullPath, forKey: "fullPath")
        aCoder.encodeBool(isEmoji, forKey: "isEmoji")

    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        chs = aDecoder.decodeObjectForKey("chs") as? String
        png = aDecoder.decodeObjectForKey("png") as? String
        type = aDecoder.decodeObjectForKey("type") as? String
        code = aDecoder.decodeObjectForKey("code") as? String
        path = aDecoder.decodeObjectForKey("path") as? String
        fullPath = aDecoder.decodeObjectForKey("fullPath") as? String
        isEmoji = aDecoder.decodeBoolForKey("isEmoji")
    }
    
    //判断是否有相同的数据 有就移除
    override func isEqual(object: AnyObject?) -> Bool {
        guard let other = object as? LBWEmoticon else {
            return super.isEqual(object)
        }
        
        if other.isEmoji == isEmoji {
            if other.isEmoji {
                //对比emoji表情重复与否,移除当前遍历到的数据模型
                
                return (other.code! as NSString).isEqualToString(code!)
            }else {
                //对比默认表情,移除当前遍历到的模型
                return (other.chs! as NSString).isEqualToString(chs!)
                
            }
            
        }
        return super.isEqual(object)
    }
    

}

















